<template>
  <div class="com-container">
    <div class="title" :style="comStyle">
      <span class="show-title" @click="showChoice = !showChoice">{{'▎ ' + showTitle}}</span>
      <span class="iconfont title-icon" :style="comStyle" @click="showChoice = !showChoice">
        <span v-show="!showChoice">&#xe6eb;</span>
        <span v-show="showChoice">&#xe6ee;</span>
      </span>
      <div class="select-con" v-show="showChoice" :style="marginStyle">
        <div class="select-item" v-for="item in selectTypes" :key="item.key" @click="handleSelect(item.key)">
          {{item.text}}
        </div>
      </div>
    </div>
    <div class="com-chart" ref="trend_ref"></div>
  </div>
</template>

<script>
import { getThemeValue } from '../utils/theme_utils';
import { mapState } from 'vuex';
export default {
  data() {
    return {
      chartInstance: null,
      allData: null, // 从服务器中获取的所有数据
      showChoice: false, // 是否显示可选项
      choiceType: 'map', // 显示的数据类型
      titleFontSize: 0, // 指名标题的字体大小
    };
  },
  created() {
    // 在组件创建完成之后，进行回调函数的注册
    this.$socket.registerCallBack('trendData', this.getData);
  },
  watch: {
    theme() {
      this.chartInstance.dispose(); // 销毁当前的图表
      // 重新以最新的主题名称来初始化图表对象
      this.initChart();
      // 完成屏幕的适配
      this.screenAdapter();
      // 更新图表的展示
      this.updateChart();
    },
  },
  mounted() {
    this.initChart();
    // this.getData();
    // 发送数据给服务器，告诉服务器，我现在需要数据
    this.$socket.send({
      action: 'getData',
      socketType: 'trendData',
      chartName: 'trend',
      value: '',
    });
    window.addEventListener('resize', this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter);
    // 在组件销毁的时候，进行回调函数的取消
    this.$socket.unRegisterCallBack('trendData');
  },
  computed: {
    selectTypes() {
      if (!this.allData) {
        return [];
      } else {
        // 把当前选中的显示数据类型剔除在外(当前选中的不在可选项当中)
        return this.allData.type.filter((item) => item.key !== this.choiceType);
      }
    },
    showTitle() {
      if (!this.allData) {
        return '';
      } else {
        return this.allData[this.choiceType].title;
      }
    },
    // 设置给标题的样式
    comStyle() {
      return {
        fontSize: this.titleFontSize + 'px',
        color: getThemeValue(this.theme).titleColor,
      };
    },
    marginStyle() {
      return {
        marginLeft: this.titleFontSize + 'px',
      };
    },
    ...mapState(['theme']),
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.trend_ref, this.theme);
      const initOption = {
        // 配置坐标轴的大小
        grid: {
          left: '3%',
          top: '35%',
          right: '4%',
          bottom: '1%',
          containLabel: true,
        },
        tooltip: {
          trigger: 'axis',
        },
        legend: {
          left: 20,
          top: '15%',
          icon: 'circle',
        },
        xAxis: {
          type: 'category',
          boundaryGap: false, // 配置紧挨边缘
        },
        yAxis: {
          type: 'value',
        },
      };
      this.chartInstance.setOption(initOption);
    },
    // 这个res就是服务端发送给客户端的图表的数据
    async getData(res) {
      // const { data: res } = await this.$http.get('trend');
      this.allData = res;
      this.updateChart();
    },
    updateChart() {
      // 半透明的颜色值
      const colorArr1 = ['rgba(11, 168, 44, 0.5)', 'rgba(44, 110, 255, 0.5)', 'rgba(22, 242, 217, 0.5)', 'rgba(254, 33, 30, 0.5)', 'rgba(250, 105, 0, 0.5)'];
      // 全透明的颜色值
      const colorArr2 = ['rgba(11, 168, 44, 0)', 'rgba(44, 110, 255, 0)', 'rgba(22, 242, 217, 0)', 'rgba(254, 33, 30, 0)', 'rgba(250, 105, 0, 0)'];
      // 处理数据
      // x轴类目轴数据
      const timeArr = this.allData.common.month;
      // y轴的数据 series 下的数据
      const valueArr = this.allData[this.choiceType].data;
      const seriesArr = valueArr.map((item, index) => {
        return {
          type: 'line',
          data: item.data,
          stack: this.choiceType, // 数据堆叠： 数据堆叠，同个类目轴上系列配置相同的stack值后，后一个系列的值会在前一个系列的值上相加。
          name: item.name,
          areaStyle: {
            // 渐变从上往下
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index], // 半透明
              },
              {
                offset: 1,
                color: colorArr2[index], // 全透明
              },
            ]),
          },
        };
      });
      // 图例的数据
      const legendArr = valueArr.map((item) => item.name);
      const dataOption = {
        xAxis: {
          data: timeArr,
        },
        legend: {
          data: legendArr,
        },
        series: seriesArr,
      };
      this.chartInstance.setOption(dataOption);
    },
    handleSelect(currentType) {
      this.choiceType = currentType;
      this.updateChart();
      this.showChoice = false;
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 100) * 3.6;
      const adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2,
          },
        },
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
  },
};
</script>

<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;
  color: #fff;
  .title-icon {
    margin-left: 10px;
    cursor: pointer;
  }
  .select-con {
    background-color: #222733;
  }
  .select-item {
    cursor: pointer;
  }
  .show-title {
    cursor: pointer;
  }
  .select-item {
    margin-top: 1px;
  }
  .select-item:hover {
    color: #ccc;
  }
}
</style>
